package com.gduf.lsm.service;

import cn.hutool.core.util.ObjectUtil;
import com.gduf.lsm.common.constant.Constants;
import com.gduf.lsm.common.constant.WaterBillTypeConstant;
import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.common.enums.RoleEnum;
import com.gduf.lsm.entity.Account;
import com.gduf.lsm.entity.User;
import com.gduf.lsm.entity.WaterBill;
import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.UserMapper;
import com.gduf.lsm.mapper.WaterBillMapper;
import com.gduf.lsm.pojo.dto.MoneyDTO;
import com.gduf.lsm.pojo.dto.VoluTimeDTO;
import com.gduf.lsm.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.undo.CannotUndoException;
import java.util.List;

/**
 * @Author lsm
 * @Data
 */
@Service
public class UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WaterBillMapper waterBillMapper;

    /**
     * 新增
     */
    public void add(User user) {
        //根据名字查询是否名称重复
        User dbUser = userMapper.selectByUsername(user.getUsername());
        //用户存在、密码为空、用户名为空抛出错误
        if (ObjectUtil.isNotNull(dbUser)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        //若为空设置默认密码
        if (ObjectUtil.isEmpty(user.getPassword())) {
            user.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        //若为空设置默认用户名
        if (ObjectUtil.isEmpty(user.getName())) {
            user.setName(Constants.USER_DEFAULT_NAME);
        }
        //设置用户角色
        user.setRole(RoleEnum.USER.name());
        userMapper.insert(user);
    }

    /**
     * 删除单个
     */
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            userMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(User user) {
        userMapper.updateById(user);
    }

    /**
     * 根据ID查询
     */
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<User> selectAll(User user) {
        return userMapper.selectAll(user);
    }

    /**
     * 分页查询
     */
    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectAll(user);
        return PageInfo.of(list);
    }


    /**
     * 登录
     * @param account
     * @return
     */
    public Account login(Account account) {
        //先查询是否有此用户
        User us=userMapper.selectByUsername(account.getUsername());
        //Account dbAdmin = adminMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(us)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        //密码对得上
        if (!account.getPassword().equals(us.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 生成token(用户ID和USER)
        String tokenData = us.getId() + "-" + RoleEnum.USER.name();
        //生成Token
        String token = TokenUtils.createToken(tokenData, us.getPassword());
        Account ac=new Account();
        //把相应内容存进ac，并把ac中的数据返回给前端
        BeanUtils.copyProperties(us,ac);
        ac.setToken(token);
        return ac;
    }

    /**
     * 注册
     * @param account
     */
    public void register(Account account) {
        User user = new User();
        BeanUtils.copyProperties(account, user);
        add(user);
    }

    /**
     * 修改密码
     * @param account
     */
    public void updatePassword(Account account) {
        //根据id查询是否存在此用户
        User dbUser=userMapper.selectById(account.getId());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        //密码不重复
        if (!account.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
        //设置新密码
        dbUser.setPassword(account.getNewPassword());
        userMapper.updateById(dbUser);
    }

    /**
     * 用户在个人页面添加金额
     * 角色管理员处理维修换新和文化活动奖金
     * 处理流水账的分类添加
     * @param moneyDTO
     */
    public void updateMoney(MoneyDTO moneyDTO) {
        String type=moneyDTO.getType();
        Integer userid=moneyDTO.getId();
        User user=userMapper.selectById(userid);
        WaterBill waterBill= WaterBill.builder()
                .userId(moneyDTO.getId())
                .money(moneyDTO.getNewMoney())
                .build();
        float m=0;
        if(type.equals(WaterBillTypeConstant.BILL_TYPE_DETAIL_RECHARGE)){ //充值入账为增加
            m=user.getMoney()+moneyDTO.getNewMoney();
            waterBill.setType(WaterBillTypeConstant.BILL_TYPE_INCOME);
            waterBill.setTypeDetail(WaterBillTypeConstant.BILL_TYPE_DETAIL_RECHARGE);
            waterBill.setMatter("用户充值入账");
        }else if(type.equals(WaterBillTypeConstant.BILL_TYPE_DETAIL_ACTIVITY)){ //活动娱乐为增加
            m=user.getMoney()+moneyDTO.getNewMoney();
            waterBill.setType(WaterBillTypeConstant.BILL_TYPE_INCOME);
            waterBill.setTypeDetail(WaterBillTypeConstant.BILL_TYPE_DETAIL_ACTIVITY);
            waterBill.setMatter("文化活动奖金入账");
        }else if(type.equals(WaterBillTypeConstant.BILL_TYPE_DETAIL_SERVE)){ //服务活动减少
            m=user.getMoney()-moneyDTO.getNewMoney();
            waterBill.setType(WaterBillTypeConstant.BILL_TYPE_EXPAND);
            waterBill.setTypeDetail(WaterBillTypeConstant.BILL_TYPE_DETAIL_SERVE);
            waterBill.setMatter("服务活动预约固定费用消耗");
        }else if(type.equals(WaterBillTypeConstant.BILL_TYPE_DETAIL_MAINTAIN)){ //维修换新减少
            m=user.getMoney()-moneyDTO.getNewMoney();
            waterBill.setType(WaterBillTypeConstant.BILL_TYPE_EXPAND);
            waterBill.setTypeDetail(WaterBillTypeConstant.BILL_TYPE_DETAIL_MAINTAIN);
            waterBill.setMatter("用户维修换新扣费");
        }else if (type.equals(WaterBillTypeConstant.BILL_TYPE_DETAIL_CANCEL)){
            m=user.getMoney()+moneyDTO.getNewMoney();
            waterBill.setType(WaterBillTypeConstant.BILL_TYPE_INCOME);
            waterBill.setTypeDetail(WaterBillTypeConstant.BILL_TYPE_DETAIL_CANCEL);
            waterBill.setMatter("用户取消预约服务固定费用退款");
        }else {
            throw new CustomException(ResultCodeEnum.APPLY_NUM_TYPE_ERROR);
        }
        userMapper.updateMoney(userid,m);
        waterBillMapper.insert(waterBill);
    }

    /**
     * 处理志愿时长
     * @param voluTimeDTO
     */
    public void updateVoluTime(VoluTimeDTO voluTimeDTO) {
        System.out.println("****voluTimeDTO****"+voluTimeDTO);
        Integer userId=voluTimeDTO.getId();
        User user=userMapper.selectById(userId);
        float v=user.getVoluTime()+voluTimeDTO.getNewTime();
        userMapper.updateVoluTime(userId,v);
    }
}
